package hyj.qx.kuaican.model;

import hyj.qx.kuaican.model.ItemType;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class Item {
    private String id;
    private String name;
    private double price;
    private ItemType type;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Item item = (Item) o;
        return Double.compare(price, item.price) == 0 && Objects.equals(id, item.id) && Objects.equals(name, item.name) && type == item.type;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, price, type);
    }

    @Override
    public String toString() {
        return "Item{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", type=" + type +
                '}';
    }

    private static final Map<String, Item> ITEMS = new HashMap<>();

    static {
        addItem(new Item("H001", "汉堡", 15.0, ItemType.MAIN));
        addItem(new Item("F002", "薯条", 15.0, ItemType.MAIN));
        addItem(new Item("W003", "鸡肉卷", 12.0, ItemType.MAIN));
        addItem(new Item("C004", "冰咖啡", 10.0, ItemType.DRINK));
        addItem(new Item("C005", "可乐", 8.0, ItemType.DRINK));
        addItem(new Item("J006", "橙汁", 9.0, ItemType.DRINK));
        addItem(new Item("D007", "冰淇淋", 6.0, ItemType.DRINK));
        addItem(new Item("D008", "蛋糕", 12.0, ItemType.DESSERT));
    }

    // 无参构造方法
    public Item() {}

    // 全参构造方法（含校验）
    public Item(String id, String name, double price, ItemType type) {
        if (id == null || name == null) {
            throw new IllegalArgumentException("ID和名称不能为null");
        }
        if (price < 0) {
            throw new IllegalArgumentException("价格不能为负数");
        }
        this.id = id;
        this.name = name;
        this.price = price;
        this.type = type;
    }

    private static void addItem(Item item) {
        ITEMS.put(item.getId(), item);
    }

    // 静态方法查询价格
    public static double getPriceById(String id) {
        Item item = ITEMS.get(id);
        if (item == null) {
            throw new IllegalArgumentException("未找到ID为 " + id + " 的商品");
        }
        return item.getPrice();
    }

    // Getter和Setter
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public double getPrice() { return price; }
    public void setPrice(double price) { this.price = price; }
    public ItemType getType() { return type; }
    public void setType(ItemType type) { this.type = type; }
}